<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\facade\Cache;

class Good extends BaseController
{
    public function cate()
    {
        $parentid=Request::get('id',0);
        $cate=Db::table('ecm_category')->order('sort')->where('parentid','=',$parentid)->select();
        //方案一：按照父辈ID进行判断
        $cateal=Db::table('ecm_category')->distinct(true)->column('parentid');
        View::assign('cateal',$cateal);

        View::assign('cate',$cate);
        return view();
    }

    public function cateedit()
    {
        return view();
    }

    public function goodAdd()
    {
        return view();
    }

    public function goodList()
    {

        $post=Request::param();

        $where=[];
        if(isset($post['keywords']) && !empty($post['keywords']))                       $where[]=['g.name','like',"%{$post['keywords']}%",];
        if(isset($post['is_show']) && $post['is_show']<>'')     $where[]=['is_show','=',$post['is_show'],];
        if(isset($post['is_hot']) && $post['is_hot']<>'')       $where[]=['is_hot','=',$post['is_hot'],];

        if(isset($post['is_down']) && count($post['is_down'])<2)$where[]=['is_down','in',$post['is_down']];

        if (isset($post['addtime']) && $post['addtime']<>'')$where[]=['g.addtime','> TIME',$post['addtime']];
        if (isset($post['addtimes']) && $post['addtimes']<>'')$where[]=['g.addtime','< TIME',$post['addtimes']];

        if (isset($post['d_price']) && $post['d_price']<>'')$where[]=['g.price','>',$post['d_price']];
        if (isset($post['g_price']) && !empty($post['g_price']))$where[]=['g.price','<',$post['g_price']];

        if (isset($post['cid']) && !empty($post['cid']))$where[]=['g.cid','=',$post['cid']];

        $list = Db::table('ecm_goods')
            ->alias('g')
            ->join('ecm_category c','g.cid = c.id')
            ->field('g.id,g.name,c.name as cname,price,photo_x,g.addtime,is_show,is_hot,is_down')
            ->where($where)
            ->paginate(['list_rows'=>5, 'query'=>$post]);
        View::assign('list',$list);
        View::assign($post);

        //使用缓存
        if(Cache::has('cate')){ //判断缓存变量是否存在
            $cateAll = Cache::get('cate');  //获取缓存数据
        }else{
            $cateAll = Db::table('ecm_category')->select()->toArray();
            Cache::set('cate',$cateAll,100); //设置缓存变量
        }
        //2.1
        //$cate = [];
        ////循环所有分类  $v为每条分类
        //foreach ($cateAll as $v){
        //    //判断 分类中parentid字段等于0 进入判断结果 都是一级分类
        //    if($v['parentid'] == 0){
        //        //将$v一级分类赋值到空数组中  索引为自己的ID
        //        $cate[$v['id']] = $v;
        //        //循环所有分类
        //        foreach ($cateAll as $vv){
        //            //如果分类的parentid等于上一级分类(一级分类)ID 进入判断里的是二级分类
        //            if($vv['parentid'] == $v['id']){
        //                //将二级分类赋值到数组  根据索引($v.id) 将一级和二级绑定到一起
        //                $cate[$v['id']]['son'][$vv['id']] = $vv;
        //                //.......无限分类循环
        //            }
        //        }
        //    }
        //}

        //2.2
        $cate = $this->getCate($cateAll,0);
        View::assign('cate',$cate);

        return view();
    }

    public function getCate($cateAll,$parentid)
    {
        $cate = [];
        foreach ($cateAll as $v){
            if($v['parentid'] == $parentid){
                $cate[$v['id']] = $v;
                $cate[$v['id']]['son'] = $this->getCate($cateAll,$v['id']);
            }
        }
        return $cate;
    }

    public function goodEdit()
    {
        return view();
    }

    public function pinpaiList()
    {
        $post = Request::param();

        $whereOr=[];
        $where=[];
        if (isset($post['keywords']) && $post['keywords']<>'') $where[]=['brand_name','like',"%{$post['keywords']}%"];
        if (isset($post['if_show']) && count($post['if_show'])==1) $where[]=['if_show','in',$post['if_show']];
        if (isset($post['recommended']) && $post['recommended']<>'') $where[]=['recommended','=',$post['recommended']];
        if (isset($post['brand_logo']) && count($post['brand_logo'])==1){
            if ($post['brand_logo'][0]==1){
                $where[]=['brand_logo','<>',''];
            }
            if ($post['brand_logo'][0]==0){
                $whereOr[]=['brand_logo','=',null];
                $whereOr[]=['brand_logo','=',''];
            }
        }
        $pinp = Db::table('ecm_brand')
            ->where($where)
            ->where(function ($q) use($whereOr){
                $q->whereOr($whereOr);})
            ->paginate(['list_rows'=>5,'query'=>$post]);
        View::assign('pinp',$pinp);
        View::assign($post);

        return view();
    }

}










